/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 4/7/2011 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using BusinessObject;
using Utility;
using System.Xml;
namespace DataAccessLayer
{
    /// <summary>
    /// Define HinhAnhMatHangDaoBase class. This class user for DataAccessLayer, contain 4 function Insert, Update, Search, Delete.
    /// If you want add new function, you must define in HinhAnhMatHangDao class , not here.
    /// </summary>
    public class HinhAnhMatHangDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public HinhAnhMatHangDaoBase() { }
        static HinhAnhMatHangDaoBase()
        {
            //Init logger.
            logger = LoggingUtility.GetLogger(typeof(HinhAnhMatHangDaoBase).FullName);
        }
        #endregion
        /// <summary>
        /// Read data from IDataReader, convert to HinhAnhMatHang Object.
        /// </summary>
        public static HinhAnhMatHang GetHinhAnhMatHangByDataReader(IDataReader reader)
        {
            String methodName = "GetHinhAnhMatHangByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                HinhAnhMatHang result = new HinhAnhMatHang();
                //read Id field data.

                result.Id = ManagerDao.GetintDataFromDataReader(reader, "Id");

                //read MaMatHang field data.

                result.MaMatHang = ManagerDao.GetstringDataFromDataReader(reader, "MaMatHang");

                //read ImageUrl field data.

                result.ImageUrl = ManagerDao.GetstringDataFromDataReader(reader, "ImageUrl");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        /// <summary>
        /// Define function Insert for HinhAnhMatHang Object.
        /// </summary>
        /// <param name="obj">Object Insert.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
        public static bool Insert(HinhAnhMatHang obj)
        {
            bool result = false;
            String methodName = "InsertHinhAnhMatHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spInsertHinhAnhMatHang");

            //Add value "MaMatHang"  paramater.
            ManagerDao.AddInParameter(cmd, "MaMatHang", DbType.AnsiString,
                ConvertValueObject.CastObjValueToInsertDB(obj.MaMatHang));
            //Add value "ImageUrl"  paramater.
            ManagerDao.AddInParameter(cmd, "ImageUrl", DbType.AnsiString,
                ConvertValueObject.CastObjValueToInsertDB(obj.ImageUrl));
            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                Int32 id = Int32.Parse(ManagerDao.GetParameterValue(cmd, "returnValue").ToString());

                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }

        /// <summary>
        /// Define function Update for HinhAnhMatHang Object.
        /// </summary>
        /// <param name="obj">Object Update.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
        public static bool Update(HinhAnhMatHang obj)
        {
            bool result = false;
            String methodName = "UpdateHinhAnhMatHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spUpdateHinhAnhMatHang");


            //Add value "Id"  paramater.
            ManagerDao.AddInParameter(cmd, "Id", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(obj.Id));
            //Add value "MaMatHang"  paramater.
            ManagerDao.AddInParameter(cmd, "MaMatHang", DbType.AnsiString, ConvertValueObject.CastObjValueToInsertDB(obj.MaMatHang));
            //Add value "ImageUrl"  paramater.
            ManagerDao.AddInParameter(cmd, "ImageUrl", DbType.AnsiString, ConvertValueObject.CastObjValueToInsertDB(obj.ImageUrl));
            // add parameter store return value.
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                int rows = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (rows == 1)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }

            }
            catch (Exception e)
            {
                //write error Message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }
        /// <summary>
        /// Get Data HinhAnhMatHang object. 
        /// </summary>
        /// <param name="id" >This is primary key to get data.</param>
        /// <returns>HinhAnhMatHang object.</returns>

        public static HinhAnhMatHang Get(int id)
        {
            String methodName = "GetHinhAnhMatHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                HinhAnhMatHang result = null;
                DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spGetHinhAnhMatHang");
                //Add value "Id"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "Id", DbType.Int32, id);
                IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
                if (reader.Read())
                {
                    result = GetHinhAnhMatHangByDataReader(reader);

                }
                reader.Close();
                return result;
            }
            catch (System.Exception e)
            {
                //write error message to log file.
                logger.Error("Get", e);
                return null;
            }
        }
        /// <summary>
        /// Get All HinhAnhMatHang object from table HinhAnhMatHang.
        /// </summary>
        /// <returns>List HinhAnhMatHang object.</returns>
        public static List<HinhAnhMatHangResult> GetAll()
        {
            String methodName = "GetAll";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<HinhAnhMatHangResult> listHinhAnhMatHang = new List<HinhAnhMatHangResult>();
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetAllHinhAnhMatHang");

                using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        listHinhAnhMatHang.Add(GetHinhAnhMatHangResultByDataReader(reader));
                    }
                    reader.Close();
                }

                return listHinhAnhMatHang;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }

        /// <summary>
        /// Search HinhAnhMatHang object in table HinhAnhMatHang with condition is t object.
        /// </summary>
        /// <param name="t">Condition for Search.</param>       
        /// <param name="totalRow">  total records in this table.</param>
        /// <returns>List HinhAnhMatHangResult object.</returns>
        public static List<HinhAnhMatHangResult> SearchHinhAnhMatHangSomeCondition(HinhAnhMatHang t)
        {
            String methodName = "SearchHinhAnhMatHangSomeCondition";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<HinhAnhMatHangResult> listHinhAnhMatHang = new List<HinhAnhMatHangResult>();
                DbCommand cmd = ManagerDao.GetStoredProcCommand("spSearchHinhAnhMatHangByCondition");
                //Add value Condition  paramater.
                ManagerDao.AddInParameter(cmd, "Condition", DbType.String, ConvertValueObject.CastObjValueToInsertDB(t.Condition));

                //Add value PageIndex  paramater.
                ManagerDao.AddInParameter(cmd, "PageIndex", DbType.Int32, t.ObjectPaging.PageIndex);
                //Add value PageSize  paramater.
                ManagerDao.AddInParameter(cmd, "PageSize", DbType.Int32, t.ObjectPaging.PageSize);
                //Add value SortField  paramater.
                ManagerDao.AddInParameter(cmd, "SortField", DbType.String, t.ObjectSortOrder.SortField);
                //Add value SortExpression  paramater.
                ManagerDao.AddInParameter(cmd, "SortExpression", DbType.String, t.ObjectSortOrder.SortExpression);
                using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        listHinhAnhMatHang.Add(GetHinhAnhMatHangResultByDataReader(reader));
                        if (t.ToltalRow <= 0)
                            t.ToltalRow = ManagerDao.Getint32DataFromDataReader(reader, "TotalRow");
                    }

                    reader.Close();
                }

                return listHinhAnhMatHang;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
        /// <summary>
        /// Delete HinhAnhMatHang object. 
        /// </summary>
        /// <param name="Id" >This is primary key to delete.</param>
        /// <returns>True/False.</returns>

        public static bool Delete(int Id)
        {
            String methodName = "DeleteHinhAnhMatHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spDeleteHinhAnhMatHang");
                //Add value "Id"  paramater.
                ManagerDao.AddInParameter(dbCommand, "Id", DbType.Int32, Id);
                //Add value returnValue  paramater.
                ManagerDao.AddParameter(dbCommand, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
                try
                {
                    ManagerDao.ExecuteNonQuery(dbCommand);
                    int parameterValue = (int)ManagerDao.GetParameterValue(dbCommand, "returnValue");
                    if (parameterValue == 1)
                    {
                        return true;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return false;
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                return false;
            }
        }
        /// <summary>
        /// Read data from IDataReader, convert to HinhAnhMatHangResult Object.
        /// </summary>
        private static HinhAnhMatHangResult GetHinhAnhMatHangResultByDataReaderByLanguage(IDataReader reader, string LanguageId)
        {
            String methodName = "GetHinhAnhMatHangResultByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                HinhAnhMatHangResult result = new HinhAnhMatHangResult();
                //read Id field data.
                result.Id = ManagerDao.GetintDataFromDataReader(reader, "Id");

                //read MaMatHang field data.
                result.MaMatHang = ManagerDao.GetstringDataFromDataReader(reader, "MaMatHang");

                //read ImageUrl field data.
                result.ImageUrl = ManagerDao.GetstringDataFromDataReader(reader, "ImageUrl");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        /// <summary>
        /// Read data from IDataReader, convert to HinhAnhMatHangResult Object.
        /// </summary>
        public static HinhAnhMatHangResult GetHinhAnhMatHangResultByDataReader(IDataReader reader)
        {
            String methodName = "GetHinhAnhMatHangResultByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                HinhAnhMatHangResult result = new HinhAnhMatHangResult();
                //read Id field data.
                result.Id = ManagerDao.GetintDataFromDataReader(reader, "Id");

                //read MaMatHang field data.
                result.MaMatHang = ManagerDao.GetstringDataFromDataReader(reader, "MaMatHang");

                //read ImageUrl field data.
                result.ImageUrl = ManagerDao.GetstringDataFromDataReader(reader, "ImageUrl");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }

    }
}